package java课题设计.功能类;
import java课题设计.工具类.Seek;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
//该类用于绘制APP主画面以及用户选择登陆或者注册的代码逻辑
public class AppJFrame extends JFrame implements ActionListener, MouseListener, DocumentListener {
    JLabel LoginText=new JLabel(new ImageIcon("D:\\登录.png"));
    JLabel registerText=new JLabel(new ImageIcon("D:\\注册.png"));
    JLabel BackGround=new JLabel(new ImageIcon("D:\\背景图.png"));
    String account,word;
    public AppJFrame(){
        extracted();
        initImage();


        this.setVisible(true);
    }
    private void extracted() {
        //设置登录界面的长和宽
        this.setSize(800, 700);
        this.setTitle("小葵花学生管理系统");
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        this.setLocationRelativeTo(null);

        //添加监听处理鼠标事件
        LoginText.addMouseListener(this);
        registerText.addMouseListener(this);
        BackGround.addMouseListener(this);
    }

    private void initImage(){
        //绘制总标题

        Font font=new Font("宋体",Font.BOLD,30);
        JLabel tetle=new JLabel("Welcome!小葵花学生管理系统");
        tetle.setForeground(Color.white);
        tetle.setBounds(210,56,450,200);
        tetle.setFont(font);
        this.getContentPane().add(tetle);



        //添加登录按钮
        LoginText.setBounds(200,450,128,47);
        LoginText.setBorder(BorderFactory.createLineBorder(Color.PINK));
        this.getContentPane().add(LoginText);

        //添加注册按钮
        registerText.setBounds(450,450,128,47);
        registerText.setBorder(BorderFactory.createLineBorder(Color.PINK));
        this.getContentPane().add(registerText);

        //添加用户名输入框
        JTextField username=new JTextField();
        username.setBorder(BorderFactory.createLineBorder(Color.pink));
        username.setBounds(300,260,300,45);
        this.getContentPane().add(username);
        //添加焦点事件监听器获取输入框中的数据
        username.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                account=username.getText();//getText方法获取用户输入的文本内容
            }
        });


        //添加密码输入框
        JTextField password=new JTextField();
        password.setBorder(BorderFactory.createLineBorder(Color.pink));
        password.setBounds(300,360,300,45);
        this.getContentPane().add(password);
        //添加观察文本组件中文本变化的事件监听器获取输入框中的数据
        password.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                word = password.getText();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                word = password.getText();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                word = password.getText();
            }
        });

        //添加账号输入提示
        JLabel Login=new JLabel("账号");
        Login.setForeground(Color.white);
        Login.setBounds(215,260,300,45);
        Font f=new Font("宋体",Font.BOLD,30);
        Login.setFont(f);
        this.getContentPane().add(Login);

        //添加密码输入提示
        JLabel pass=new JLabel("密码");
        pass.setForeground(Color.white);
        pass.setBounds(215,360,300,45);
        f=new Font("宋体",Font.BOLD,30);
        pass.setFont(f);
        this.getContentPane().add(pass);
        //添加背景图
        BackGround.setBounds(0,0,800,700);
        this.getContentPane().add(BackGround);

    }


    //动作监听方法
    @Override
    public void actionPerformed(ActionEvent e) {

    }


    @Override
    public void mouseClicked(MouseEvent e)  {
        Object obj=e.getSource();//获取触发事件对象的引用
        if(obj==LoginText) {
            //文本库检索登录用户名和密码的正确性
            //如果正确，隐藏当前窗口进入下一步窗口
            //如果错误，提示密码或用户名输入有误
            //System.out.println("点击的是登录按钮");
            //System.out.println(account+" "+word);
            String str = account + "&" + word;
            boolean flag;
            try {
                flag = Seek.seek(str);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            if (flag == false) {
                //唤起登陆后的窗口
                System.out.println("登陆成功");
                this.setVisible(false);
                //关闭当前页面后唤醒登录之后的页面
                new OperationPage();
            } else {
                System.out.println("登陆失败");
                //跳出弹窗提示账号或密码有误
                JDialog wrong = new JDialog(this, null, true);
                wrong.setBounds(550, 300, 300, 200);
                wrong.setTitle("登陆失败");
                JLabel w = new JLabel("账号或密码输入有误,登陆失败");
                w.setFont(new Font("宋体", Font.BOLD, 18));
                w.setBounds(100, 100, 100, 100);
                wrong.add(w);
                wrong.setAlwaysOnTop(true);
                wrong.setVisible(true);
            }
        }
        else if(obj==registerText){
            //唤起注册程序
            System.out.println("点击的是注册按钮");
            new Register(AppJFrame.this);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void insertUpdate(DocumentEvent e) {
    }

    @Override
    public void removeUpdate(DocumentEvent e) {

    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }
}
